package com.example.a31210.mylive;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.gensee.common.ServiceType;
import com.gensee.entity.InitParam;
import com.gensee.fastsdk.GenseeLive;
import com.gensee.fastsdk.core.GSFastConfig;
import com.gensee.fastsdk.entity.GSMoreItem;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by YuShuangPing on 2018/11/1.
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Spinner spinner_publish_watch;//观看端或者发布端
    private Spinner spinner_webcast_training;//webcast,training
    private Spinner spinner_watch_portrait_horiented;//观看端模式选择，竖屏，横屏
    private EditText et_domain;//域名
    private EditText et_room_number;//房间号
    private EditText et_login_account;//站点账户
    private EditText et_login_psw;//站点密码
    private EditText et_nickName;//昵称
    private EditText et_join_psw;//口令
    private EditText et_k;//第三方认证的k值
    private Button btn_live;
    private boolean isPublishMode;//是否是发布端
    private ServiceType serviceType;
    private  int watchScreenMode;//观看端模式选择

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner_publish_watch=findViewById(R.id.spinner_publish_watch);
        spinner_webcast_training=findViewById(R.id.spinner_webcast_training);
        spinner_watch_portrait_horiented=findViewById(R.id.spinner_watch_portrait_horiented);
        et_domain=findViewById(R.id.et_domain);
        et_room_number=findViewById(R.id.et_room_number);
        et_login_account=findViewById(R.id.et_login_account);
        et_login_psw=findViewById(R.id.et_login_psw);
        et_nickName=findViewById(R.id.et_nickName);
        et_join_psw=findViewById(R.id.et_join_psw);
        et_k=findViewById(R.id.et_k);
        initPublishWatch();
        initWebcastTraining();
        initWatchOrientation();
        btn_live=findViewById(R.id.btn_live);
        btn_live.setOnClickListener(this);
        
    }
    //观看端模式选择
    private void initWatchOrientation() {
        final List<String> watchModelist = new ArrayList<String>();
        watchModelist.add("分屏观看(文档+视频)");
        watchModelist.add("竖屏观看");
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, watchModelist);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_watch_portrait_horiented.setAdapter(adapter);
        spinner_watch_portrait_horiented.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0://分屏观看(文档+视频
                        watchScreenMode=GSFastConfig.WATCH_SCREEN_MODE_VIDEO_DOC;
                        break;
                    case 1://竖屏观看
                        watchScreenMode=GSFastConfig.WATCH_SCREEN_MODE_PORTRAIT;
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    //webcast,training
    private void initWebcastTraining() {
        List<String>list=new ArrayList<>();
        list.add("webcast");
        list.add("training");
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_webcast_training.setAdapter(adapter);
        spinner_webcast_training.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        serviceType = ServiceType.WEBCAST;
                        break;
                    case 1:
                        serviceType = ServiceType.TRAINING;
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    //发布端，观看端
    private void initPublishWatch() {
        List<String>list=new ArrayList<>();
        list.add("观看端");
        list.add("发布端");
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_publish_watch.setAdapter(adapter);
        spinner_publish_watch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i){
                    case 0://观看端
                        isPublishMode=false;
                        spinner_watch_portrait_horiented.setVisibility(View.VISIBLE);
                        break;
                    case 1://发布端
                        isPublishMode=true;
                        spinner_watch_portrait_horiented.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        InitParam initParam = new InitParam();
        //若一个url为http://test.gensee.com/site/webcast   域名是“test.gensee.com”
        initParam.setDomain(et_domain.getText().toString());
        //设置对应编号，如果是点播则是点播编号，是直播便是直播编号。
        //请注意不要将id理解为编号。
        //作用等价于id，但不是id。有id可以不用编号，有编号可以不用id
        initParam.setNumber(et_room_number.getText().toString());
        //设置站点认证账号 即登录站点的账号
        initParam.setLoginAccount("");
        //设置站点认证密码 即登录站点的密码,如果后台设置直播需要登录或点播需要登录，那么登录密码要正确  且帐号同时也必须填写正确
        initParam.setLoginPwd("");
        //设置昵称  用于直播间显示或统计   一定要填写
        initParam.setNickName(et_nickName.getText().toString());
        //可选 如果后台设置了保护密码 请填写对应的口令
        initParam.setJoinPwd(et_join_psw.getText().toString());
        //第三方认证K值，如果启用第三方集成的时候必须传入有效的K值
        initParam.setK(et_k.getText().toString());
        //若一个url为http://test.gensee.com/site/webcast ,serviceType是 ServiceType.WEBCAST,
        //url为http://test.gensee.com/site/training,serviceTypeserviceType是 ServiceType.TRAINING
        initParam.setServiceType(serviceType);

        GSFastConfig gsFastConfig = new GSFastConfig();

        gsFastConfig.setPublish(isPublishMode);//是否是主播端，false和默认观看端
        gsFastConfig.setPublishScreenMode(GSFastConfig.PUB_SCREEN_MODE_PORTRAIT);//发布端屏幕方向
        gsFastConfig.setWatchScreenMode(watchScreenMode);//观看端屏幕方向
        gsFastConfig.setHardEncode(true);//编码方式：硬编，软编
        gsFastConfig.setPubQuality(GSFastConfig.PUB_QUALITY_SD);//视频分辨率
        //配置观看端打赏固定金额面板,最多支持6个固定金额,固定金额最高不能超过200000(即2000.00元)
//        gsFastConfig.setFixedMoneyArray(arr);
        //分屏观看端,界面配置
        gsFastConfig.setShowDoc(true);//是否显示文档
        gsFastConfig.setShowChat(true);//是否显示聊天
        gsFastConfig.setShowQa(true);//是否显示问答
        gsFastConfig.setShowIntro(true);//是否显示简介
        gsFastConfig.setShowPIP(true);//是否显示全屏模式画中画
        gsFastConfig.setShowHand(true);//是否显示举手
//		gsFastConfig.setShowRateSwitch(getBooleanFromPrefrences(SettingActivity.GS_RATE));
        gsFastConfig.setShownetSwitch(true);//是否显示优选网络
        gsFastConfig.setShowDanmuBtn(true);//是否显示弹幕开关
        gsFastConfig.setShowCloseVideo(true);//是否显示关闭视频
        gsFastConfig.setSkinType(GSFastConfig.SKIN_TYPE_NIGHT);//皮肤
        GenseeLive.startLive(MainActivity.this,gsFastConfig,initParam);
    }
}
